package wwl.lsf.hellospringboot.xss;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Component;
 
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
//@Component
//@WebFilter
public class XssFilter implements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
 
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        XssAndSqlHttpServletRequestWrapper xssAndSqlHttpServletRequestWrapper = new XssAndSqlHttpServletRequestWrapper(request);
        filterChain.doFilter(xssAndSqlHttpServletRequestWrapper,servletResponse);
    }
 
    @Override
    public void destroy()
    {
 
    }
 
//    @Bean
//    @Primary
//    public ObjectMapper xssObjectMapper(Jackson2ObjectMapperBuilder builder)
//    {
//        //解析器
//        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
//        //注册xss解析器
//        SimpleModule xssModule = new SimpleModule("XssStringJonSerializer");
//        xssModule.addSerializer(new XssStringJsonSerializer());
//        objectMapper.registerModule(xssModule);
//        return objectMapper;
//    }
}